package ui;
import java.sql.SQLException;
import javax.swing.AbstractAction;

import graphics.IncubationPieChart;
import graphics.LongevityPieChart;
import graphics.PieChart;
import graphics.RiskPieChart;
import graphics.SizePieChart;

import java.awt.event.ActionEvent;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class pieChartWindow extends javax.swing.JFrame {
	private JLabel labelLabel;
	private JComboBox leOptions;
	private AbstractAction pieChartCallAction;
	private JButton generatePieChart;
	private static String[] listaOMG = new String[] { "Risk of Extinction" , "Size" ,
		"Incubation Time" , "Longevity" };

	/**
	* Auto-generated main method to display this JFrame
	*/
		
	public pieChartWindow() {
		super();
		initGUI();
	}
	
	private void generatePieChart() throws SQLException {
		if ( leOptions.getSelectedItem().toString().equals(listaOMG[0])) {
			RiskPieChart.main(null);
		} else {
			if (leOptions.getSelectedItem().toString().equals(listaOMG[1])) {
				SizePieChart.main(null);
			} else {
				if ( leOptions.getSelectedItem().toString().equals(listaOMG[2])) {
					IncubationPieChart.main(null);
				} else {
					LongevityPieChart.main(null);
				}
			}
		}
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				labelLabel = new JLabel();
				labelLabel.setText("Select the desired attribute to generate the Pie Chart:");
			}
			{
				ComboBoxModel leOptionsModel = 
					new DefaultComboBoxModel(
							listaOMG);
				leOptions = new JComboBox();
				leOptions.setModel(leOptionsModel);
			}
			{
				generatePieChart = new JButton();
				generatePieChart.setText("Generate Pie Chart");
				generatePieChart.setAction(getPieChartCallAction());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(labelLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(leOptions, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(generatePieChart, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(labelLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(leOptions, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 182, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(generatePieChart, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 182, Short.MAX_VALUE))));
			pack();
			this.setSize(341, 148);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getPieChartCallAction() {
		if(pieChartCallAction == null) {
			pieChartCallAction = new AbstractAction("Generate Pie Chart", null) {
				public void actionPerformed(ActionEvent evt) {
					try {
						generatePieChart();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
		}
		return pieChartCallAction;
	}

}
